package com.yangfei.mystudy1020.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.yangfei.mystudy1020.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EventBusMainActivity extends Activity {

    @BindView(R.id.tv_info)
    TextView mTvInfo;
    @BindView(R.id.btn_start)
    Button mBtnStart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_bus_main);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        mTvInfo.setText("ready!!!");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @OnClick(R.id.btn_start)
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_start:{
                Intent in = new Intent(EventBusMainActivity.this, EventBusActivity.class);
                EventBusMainActivity.this.startActivity(in);
                break;
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(EventBusActivity.MessageEvent event) {
        /* Do something */
        mTvInfo.setText(event.msg);
    }

}
